﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using ShoppingList.DTO;
using Silvermoon.UI;
using ShoppingList.Biz;
using Silvermoon.OpenGL;
using Silvermoon.Images;

namespace ShoppingList.Pages
{
    public class CategoryPicker : Page
    {
        private Category category;
        private ListView listView;
        private ListViewController<TitleDetailsImageItem, Category> controller;

        public CategoryPicker()
            : base()
        {
            InitializePage();
        }

        public Category Category
        {
            get { return category; }
            set { category = value; }
        }

        private void InitializePage()
        {
            Styles = StylePool.Main;
            Title = "CHOOSE CATEGORY";
            listView = new ListView { Margin = new Margin(10, 0, 10, 0), };
            SetTransition(300, TransitionMask.Fade | TransitionMask.Explode, TransitionMask.Fade | TransitionMask.Explode);
            Controls.Add(listView);

            controller = new ListViewController<TitleDetailsImageItem, Category>(listView) { ItemHeight = 79 };
            controller.UpdateItem += new EventHandler<ItemControlGeneratorEventArgs<TitleDetailsImageItem>>(controller_UpdateItem);
            controller.InitializeItem += new EventHandler<ItemControlGeneratorEventArgs<TitleDetailsImageItem>>(controller_InitializeItem);

            controller.DataSource = BizContext.Context.Categories;
        }

        void controller_InitializeItem(object sender, ItemControlGeneratorEventArgs<TitleDetailsImageItem> e)
        {
            e.Item.Tap += listView_TapItem;
            e.Item.ImageBackground = SysImages.Circle;
            e.Item.Image = ImagePool.Cart;
            e.Item.TitleLineAlignment = Alignment.Center;
            //e.Item.Alignment = Alignment.Center;
        }

        void listView_TapItem(object sender, EventArgs e)
        {
            Item item = sender as Item;
            Category = item.DataValue as Category;
            if (CategoryChosen != null) CategoryChosen(this, EventArgs.Empty);
            GoBack();
        }

        void controller_UpdateItem(object sender, ItemControlGeneratorEventArgs<TitleDetailsImageItem> e)
        {
            e.Item.Title = (e.Value as Category).Name;
        }

        protected override void OnHidden()
        {
            base.OnHidden();
        }

        public event EventHandler CategoryChosen;
    }
}
